package se19team1e.gui.awt;

import java.awt.Choice;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import se19team1e.entity.CompanyDTO;

/**
 * 
 * This is a helper choice gui element that manipulates company names to company objects.
 * 
 * @author lai shiaw san
 *
 */
@SuppressWarnings("serial")
public class CompanyComboBox extends Choice {
    
    /**
     * Map of company name to dto object for lookup.
     */
    private final Map< String, CompanyDTO > companyMap = new HashMap< String, CompanyDTO >();

    /**
     * Initialize the combo box with a new set of companies.
     * <p>
     * Previous values are all removed.
     * </p>
     * @param companies Companies to initialize the combo box with.
     */
    public void initialize( List<CompanyDTO> companies ) {
        // clear previous values
        this.companyMap.clear();
        this.removeAll();
        
        // build up new map
        for ( CompanyDTO company : companies ) {
            this.companyMap.put( company.getName(), company );
        }

        // build up new combo box values
        for ( String companyName : this.companyMap.keySet() ) {
            this.add( companyName );
        }
    }

    /**
     * Get the id of the selected company.
     * @return Selected company id.
     */
    public String getSelectedCompanyId() {
        String selectedName = this.getSelectedItem();
        CompanyDTO company = this.companyMap.get( selectedName );
        return company.getId();
    }
    
}
